# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Maintainer: Valery Kartel <valery.kartel@gmail.com>
pkgname=litespeed
pkgver=1.7.16
pkgrel=0
_libbcrypt=55ff64349dec3012cfbbb1c4f92d4dbd46920213
_boringssl=cf8d3ad3cea51cf7184307d54f465da62b7d8408
_pkgreal=open$pkgname
_pkghome=var/lib/$pkgname
_php=php8
pkgdesc="High-performance, lightweight, open source HTTP server"
url="https://openlitespeed.org/"
arch="x86 x86_64"
license="GPL-3.0-or-later"
pkgusers=litespeed
pkggroups=litespeed
depends="$_php-$pkgname $_php-bcmath $_php-session $_php-sockets $_php-posix"
makedepends="go cmake linux-headers openssl-dev geoip-dev expat-dev pcre-dev zlib-dev udns-dev autoconf
	bsd-compat-headers lua-dev luajit-dev brotli-dev brotli-static zlib-static libmaxminddb-dev"
options="!check"
install="$pkgname.pre-install"
subpackages="$pkgname-openrc $pkgname-snmp::noarch"
source="https://openlitespeed.org/packages/openlitespeed-$pkgver.src.tgz
	https://github.com/litespeedtech/libbcrypt/archive/$_libbcrypt/litespeedtech-libbcrypt-$_libbcrypt.tar.gz
	https://github.com/google/boringssl/archive/$_boringssl/google-boringssl-$_boringssl.tar.gz
	$pkgname.initd
	include.patch
	install.patch
	ls_lock.patch
	admin.patch
	"
builddir="$srcdir/openlitespeed-$pkgver"

prepare() {
	default_prepare

	rm -Rf boringssl
	mv -f "$srcdir"/boringssl-$_boringssl boringssl
	rm -Rf libbcrypt
	mv -f "$srcdir"/libbcrypt-$_libbcrypt libbcrypt
	rm -Rf "$builddir"/dist/lsrecaptcha

	cp libbcrypt/bcrypt.h include
	cp src/lshpack/lsxpack_header.h include/lsxpack_header.h
	cp src/lshpack/lshpack.h include/lshpack.h
	cp src/liblsquic/ls-qpack/lsqpack.h include/lsqpack.h

	sed -i -e "s/ln -s \/usr\/bin\/lsphp7/ln -s \/usr\/bin\/ls$_php/" dist/install.sh
}

build() {
	export LDFLAGS="$LDFLAGS -L$PWD/libbcrypt"

	(
		cd libbcrypt
		make
		mv bcrypt.a libbcrypt.a
	)

	./configure \
		--host=$CHOST \
		--build=$CBUILD \
		--prefix=/$_pkghome \
		--with-user=$pkgusers \
		--with-group=$pkggroups \
		--enable-adminssl=no \
		--enable-recaptcha=no \
		--disable-rpath \
		--disable-static \
		--with-libdir=/usr/lib \
		--with-openssl=/usr \
		--with-bssl=boringssl \
		--with-expat \
		--with-pcre \
		--with-lua \
		--with-brotli=/usr \
		--with-zlib=/lib
	local extra_libs
	case "$CARCH" in
		x86 | x86_64 | arm* | aarch64 | ppc64le ) extra_libs='-latomic'
	esac
	make EXTRA_LIBS="$extra_libs"
}

package() {
	local file;
	make DESTDIR="$pkgdir" install

	mkdir -p "$pkgdir"/usr/lib/$pkgname \
		"$pkgdir"/usr/sbin \
		"$pkgdir"/var/log

	#fix admin files
	cp -fr $builddir/dist/admin/misc/* "$pkgdir"/$_pkghome/admin/misc

	# remove trash
	rm -fr "$pkgdir"/$_pkghome/php* \
		"$pkgdir"/$_pkghome/lib \
		"$pkgdir"/$_pkghome/GPL* \
		"$pkgdir"/$_pkghome/gdata \
		"$pkgdir"/$_pkghome/autoupdate \
		"$pkgdir"/$_pkghome/fcgi-bin/* \
		"$pkgdir"/$_pkghome/bin/lshttpd \
		"$pkgdir"/$_pkghome/admin/conf/php.* \
		"$pkgdir"/$_pkghome/admin/misc/gdb-bt \
		"$pkgdir"/$_pkghome/admin/misc/convertxml.* \
		"$pkgdir"/$_pkghome/admin/misc/build_admin_php.sh

	# fix ownership
	chown -R $pkgusers:$pkggroups \
		"$pkgdir"/$_pkghome/tmp \
		"$pkgdir"/$_pkghome/conf \
		"$pkgdir"/$_pkghome/logs \
		"$pkgdir"/$_pkghome/backup \
		"$pkgdir"/$_pkghome/cachedata \
		"$pkgdir"/$_pkghome/admin/tmp \
		"$pkgdir"/$_pkghome/admin/logs \
		"$pkgdir"/$_pkghome/admin/conf \
		"$pkgdir"/$_pkghome/admin/cgid \
		"$pkgdir"/$_pkghome/Example/logs

	# install configs
	install -Dm755 "$srcdir"/$pkgname.initd \
		"$pkgdir"/etc/init.d/$pkgname
	mv "$pkgdir"/$_pkghome/conf \
		"$pkgdir"/etc/$pkgname
	mv "$pkgdir"/$_pkghome/admin/conf \
		"$pkgdir"/etc/$pkgname/admin
	ln -s /etc/$pkgname "$pkgdir"/$_pkghome/conf
	ln -s /etc/$pkgname/admin "$pkgdir"/$_pkghome/admin/conf
	find "$pkgdir"/etc/$pkgname -type f -print0 | xargs -0 chmod -x

	# install binary
	mv "$pkgdir"/$_pkghome/bin/$_pkgreal \
		"$pkgdir"/usr/sbin/lshttpd
	ln -sf /usr/sbin/lshttpd \
		"$pkgdir"/$_pkghome/bin/$pkgname

	# install modules
	_modules=$(find "$pkgdir"/$_pkghome/modules -name "*.so")
	for file in $_modules; do
		mv $file "$pkgdir"/usr/lib/$pkgname/${file##*/}
		ln -s /usr/lib/$pkgname/${file##*/} $file
	done

	# install logs
	mv "$pkgdir"/$_pkghome/logs "$pkgdir"/var/log/$pkgname
	mv "$pkgdir"/$_pkghome/admin/logs "$pkgdir"/var/log/$pkgname/admin
	mv "$pkgdir"/$_pkghome/Example/logs "$pkgdir"/var/log/$pkgname/Example
	ln -s /var/log/$pkgname "$pkgdir"/$_pkghome/logs
	ln -s /var/log/$pkgname/admin "$pkgdir"/$_pkghome/admin/logs
	ln -s /var/log/$pkgname/Example "$pkgdir"/$_pkghome/Example/logs

	# install backend
	ln -s /usr/bin/ls$_php "$pkgdir"/$_pkghome/fcgi-bin/lsphp
	ln -s /etc/$_php/php.ini "$pkgdir"/etc/$pkgname/php.ini
	ln -s /etc/$_php/php.ini "$pkgdir"/etc/$pkgname/admin/php.ini
}

snmp() {
	pkgdesc="$pkgdesc (snmp monitoring add-on + cacti templates)"
	depends="$pkgname net-snmp"

	amove $_pkghome/add-ons/snmp_monitoring
}


sha512sums="
7c4dbfaec05144f37fe1fbf6313b265290872b7e5be8cc317d738207e2b65f35c8984d05c68c636b85b35bbce198eb35ad9ea62cde6fd16b95138812d2f4b1fc  openlitespeed-1.7.16.src.tgz
7304acb02031fe2d7aaa667179b0539b30570750676c8474ea662a5505cf1b9f09d29e2927d15996fa1ee5c181f9c9aea54137b48252f82430b75a3d5e36d80b  litespeedtech-libbcrypt-55ff64349dec3012cfbbb1c4f92d4dbd46920213.tar.gz
9157eb009311ea34e3fb530f368783f3c70a1a60e4d1ccf5b8f542a23a88896a0a3dc3062568a73c616862ad40ff5e3d0c05f6e28da41fc755ff0b8761b0250c  google-boringssl-cf8d3ad3cea51cf7184307d54f465da62b7d8408.tar.gz
2b0299246aaeb885122d8f939437f0e4701f6a79ea976f725ba8765d8b295c2797c0cac30e639d5eda665b9589710ee55acc4d03b078c4b9cc1e564994ccb9f5  litespeed.initd
f2e3f33123548fa3b6ad65e989ee8ad980b428e05f62a068d8493fdefd0bc66ebf2840087411f6762cda0e9955003d0ca6fe37cca40dfd38a1fd56ffa6ef3e7d  include.patch
f60cb9888b58c81d9ba6849f9645998d25c1a1e20809b1e2ebb297e5ab573c77d065a19b1b7fc826146d94c6211ffdbaa2104ef6c213700cb3635a3e58b2ed55  install.patch
2fef9373cb70d379452003cbea98931eaeb437788c6ae671d4b6a02f81280f0f815e19f7c2e9c2c19609ee7ce16b179cf6809e53fa4d9203dac73b964d5a9579  ls_lock.patch
7bd22268f383cdd2cd71586a1ada0f0e705e84da2ef5d0aea78f6ce55504a63a057991d2be8d5b18e8af200271012436b492b842d4a4063a840358a42a5dbbb3  admin.patch
"
